package com.njxzc.igo.utils;

import java.util.ArrayList;
import java.util.List;

import com.njxzc.igo.model.Products;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.util.PropertyFilter;

public class JsonUtil {
	public static JsonConfig solveProductslazy(){
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if (arg1.equals("productType") || arg1.equals("results")) {
					return true;
				} else {
					return false;
				}
				
				
			}
		});
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });
		return config;
	}
	
	
	public static JsonConfig solveOrderLazy(){
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if (arg1.equals("user")||arg1.equals("order")|| arg1.equals("adddetails")||arg1.equals("orderdetails")) {
					return true;
				} else {
					return false;
				}
				
				
			}
		});
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });
		return config;
	}
	
	
	public static Pager JsonStringToPager(String content){
//		
//		List list = new ArrayList<>();
//		
//		JSONObject jObject = JSONArray.fromObject(content).getJSONObject(0);
//		Pager pager = (Pager) JSONObject.toBean(jObject, Pager.class);
//		JSONArray jsonArray = jObject.getJSONArray("result");
//		for (int i = 0; i < jsonArray.size(); i++) {
//			JSONObject jObject2 = jsonArray.getJSONObject(i);
//			Products product = (Products) JSONObject.toBean(jObject2, Products.class);
//			list.add(product);
//		}
//		
//		pager.setResult(list);
		List list = new ArrayList<>();
		
		JSONArray ja = JSONArray.fromObject(content);
		Pager pager = null;
		for(int index=0;index<ja.size();index++){
			JSONObject jObject = ja.getJSONObject(index);
		if(index==0){
			pager = (Pager) JSONObject.toBean(jObject, Pager.class);
		}
			
			JSONArray jsonArray = jObject.getJSONArray("result");
			for (int i = 0; i < jsonArray.size(); i++) {
				JSONObject jObject2 = jsonArray.getJSONObject(i);
				Products product = (Products) JSONObject.toBean(jObject2, Products.class);
				list.add(product);
			}
			
		}
		pager.setResult(list);
		return pager;
	}
	
	public static JsonConfig getUserJSonConfig() {
		JsonConfig cfg = new JsonConfig();
	    cfg.setJsonPropertyFilter(new PropertyFilter()
	    {
	         public boolean apply(Object source, String name, Object value) {
	           if(name.equals("addresses")||name.equals("orders")
	        		   ||name.equals("shoppingcarts")) {
	             return true;
	           } else {
	             return false;
	          }
	        }
	       });
	    
	    
		return cfg;
	}
	
	
	
	public static JsonConfig solveAddresslazy(){
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if (arg1.equals("user")) {
					return true;
				} else {
					return false;
				}
				
				
			}
		});
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });
		return config;
	}
	
	
	
	public static JsonConfig solveProvincelazy(){
		JsonConfig config = new JsonConfig();
		config.setJsonPropertyFilter(new PropertyFilter() {
			public boolean apply(Object arg0, String arg1, Object arg2) {
				if (arg1.equals("citys")) {
					return true;
				} else {
					return false;
				}
				
				
			}
		});
		config.setExcludes(new String[] { "handler", "hibernateLazyInitializer" });
		return config;
	}
}
